package com.dure.hotmemory.adapters;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.dure.commonlib.util.StringUtil;
import com.dure.commonlib.util.TimeUtils;
import com.dure.hotmemory.R;
import com.dure.hotmemory.bean.Level;
import com.dure.hotmemory.listeners.ConfirmListener;


import java.util.ArrayList;
import java.util.List;

/**
 * Created by leix on 14/12/2016.
 */

public class TopAdapter extends BaseAdapter {
    List<Level> appList = new ArrayList<>();
    Context context;
    ConfirmListener confirmListener;

    public TopAdapter(Context context) {
        this.context = context;
    }

    @Override
    public int getCount() {
        return appList.size();
    }

    @Override
    public Level getItem(int i) {
        return appList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder;
        if (view == null) {
            viewHolder = new ViewHolder();
            view = LayoutInflater.from(context).inflate(R.layout.top_level_item, null);
            viewHolder.topTV = (TextView) view.findViewById(R.id.top_tv);
            viewHolder.levelTV = (TextView) view.findViewById(R.id.level_tv);
            viewHolder.phoneTV = (TextView) view.findViewById(R.id.phone_tv);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }

        final Level app = getItem(i);

        viewHolder.topTV.setText("Top:" + (i + 1));

        viewHolder.levelTV.setText("Level:" + app.level);

        if (TextUtils.isEmpty(app.user_name)) {
            if (!TextUtils.isEmpty(app.phone))
                viewHolder.phoneTV.setText("用户:" + StringUtil.getStarPhone(app.phone));
            else{
                viewHolder.phoneTV.setText("用户: 佚名");
            }
        }else {
            viewHolder.phoneTV.setText(app.user_name);
        }

        return view;
    }

    public void setConfirmListener(ConfirmListener confirmListener) {
        this.confirmListener = confirmListener;
    }

    public void setData(List<Level> appList) {
        if (appList != null && appList.size() > 0) {
            this.appList.clear();
            addData(appList);
        }
    }

    public void addData(List<Level> appList) {
        this.appList.addAll(appList);
        notifyDataSetChanged();
    }

    class ViewHolder {
        TextView phoneTV;
        TextView topTV;
        TextView levelTV;
    }
}
